package com.xbongbong.paas.controller.mobile;/*/*/

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.common.pojo.dto.LocationGetDTO;
import com.xbongbong.pro.common.pojo.dto.LocationSetDTO;
import com.xbongbong.pro.common.pojo.vo.LocationGetVO;
import com.xbongbong.pro.common.pojo.vo.LocationSetVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.LocationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2019/5/13 15:15
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/common/location")
public class MobileLocationController {
    private static final Logger LOG = LoggerFactory.getLogger(MobileLocationController.class);

    @Resource
    private LocationService locationService;

    /**
     * Description: 获取form的经纬度
     * @author 魏荣杰
     * @date 2019/5/13 16:01
     * @since v1.0
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid LocationGetDTO locationGetDTO, BindingResult br) {
        XbbResponse<LocationGetVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LocationGetVO locationGetVO = locationService.getLocation(locationGetDTO);
                response = new XbbResponse<>(locationGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 设置form的地址字段
     * @author 魏荣杰
     * @date 2019/5/13 16:03
     * @since v1.0
     */
    @RequestMapping(value = "/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String set(@RequestBody @Valid LocationSetDTO locationSetDTO, BindingResult br) {
        XbbResponse<LocationSetVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LocationSetVO locationSetVO = locationService.setLocation(locationSetDTO);
                response = new XbbResponse<>(locationSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
